import { defineStore } from "pinia"
import { loginApi, userInfo} from "../api/modules/user"
export const UserStore = defineStore('UserStore', {
	state: () => ({
		userinfo: {},
		token: 	uni.getStorageSync('TOKEN') || '',
		id: uni.getStorageSync('ID') || '',
		name: uni.getStorageSync('NAME') || '',
	}),
	actions: {
		// 登录
		async login(user) {
			const data = await loginApi(user)
			uni.setStorageSync('TOKEN', data[1])
			uni.setStorageSync('NAME', data[0].name)
			uni.setStorageSync('ID', data[0].id)
		},
		// 获取用户消息
		async getUserinfo(i) {
			const  data  = await userInfo(i)
			this.userinfo = data[0]
		},
		// 退出登录
		async userLogout() {
			this.clearUser()
		},
		clearUser() {
			uni.removeStorageSync('TOKEN')
			this.token = ''
			this.userinfo = {}
			console.log(data[0].name,data[0].id);
			uni.setStorageSync('TOKEN', data[1])
			uni.setStorageSync('NAME', data[0].name)
			uni.setStorageSync('ID', data[0].id)
		}
	},
	getters: {}
})